package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.constants.Constants;
import com.itbaizhan.openhis.domain.Menu;
import com.itbaizhan.openhis.domain.SimpleUser;
import com.itbaizhan.openhis.dto.MenuDto;
import com.itbaizhan.openhis.service.MenuService;
import com.itbaizhan.openhis.mapper.MenuMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @author MR
* @description 针对表【sys_menu(菜单权限表)】的数据库操作Service实现
* @createDate 2025-06-29 21:52:25
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{
	
	@Autowired
	private MenuMapper menuMapper;
	
	/**
	 * 查询所有菜单及权限
	 * @param menuDto
	 * @return
	 */
	@Override
	public List<Menu> listAllMenus(MenuDto menuDto) {
		QueryWrapper<Menu> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(menuDto.getMenuName()),
				Menu.COL_MENU_NAME, menuDto.getMenuName());
		wrapper.eq(StringUtils.isNotBlank(menuDto.getStatus()),
				Menu.COL_STATUS, menuDto.getStatus());
		
		return menuMapper.selectList(wrapper);
	}
	
	/**
	 * 根据角色Id查询菜单Id
	 * @param roleId
	 * @return
	 */
	@Override
	public List<Long> getMenuIdsByRoleId(Long roleId) {
		return menuMapper.queryMenuIdsByRoleId(roleId);
	}
	
	/**
	 * 查询菜单信息
	 * @param isAdmin 是否是超级管理员
	 *   =true  simpleUser可以为空
	 * @return
	 */
	@Override
	public List<Menu> selectMenuTree(boolean isAdmin) {
		QueryWrapper<Menu> wrapper = new QueryWrapper<>();
		wrapper.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
		wrapper.in(Menu.COL_MENU_TYPE,Constants.MENU_TYPE_C,Constants.MENU_TYPE_M,
				Constants.MENU_TYPE_F);
		wrapper.orderByAsc(Menu.COL_PARENT_ID);
		if(isAdmin){
			return menuMapper.selectList(wrapper);
		}else{
			return menuMapper.selectList(wrapper);
		}
	}
	
	/**
	 * 检验菜单权限下是否有子菜单
	 * @param menuId
	 * @return
	 */
	@Override
	public boolean hasChildByMenuId(Long menuId) {
		int count =menuMapper.queryChildByMenuId(menuId);
		return count > 0 ? true : false;
	}
	
	/**
	 * 根据菜单id查询菜单信息
	 * @param menuId
	 * @return
	 */
	@Override
	public Menu getOne(Long menuId) {
		return menuMapper.selectById(menuId);
	}
	
	/**
	 * 添加菜单权限
	 * @param menuDto
	 * @return
	 */
	@Override
	public int addMenu(MenuDto menuDto) {
		Menu menu = new Menu();
		BeanUtils.copyProperties(menuDto, menu);
		menu.setCreateBy(menuDto.getSimpleUser().getUserName());
		menu.setCreateTime(DateUtil.date());
		return menuMapper.insert(menu);
	}
	
	/**
	 * 修改菜单权限
	 * @param menuDto
	 * @return
	 */
	@Override
	public int updateMenu(MenuDto menuDto) {
		Menu menu = new Menu();
		BeanUtils.copyProperties(menuDto, menu);
		menu.setUpdateBy(menuDto.getSimpleUser().getUserName());
		menu.setUpdateTime(DateUtil.date());
		return menuMapper.updateById(menu);
	}
	
	/**
	 * 删除菜单权限
	 * @param menuId
	 * @return
	 */
	@Override
	public int deleteMenuById(Long menuId) {
		//先删除sys_role_menu表关联数据数据
		menuMapper.deleteRoleMenuByMenuIds(Arrays.asList(menuId));
		//再删除sys_menu表数据
		return menuMapper.deleteById(menuId);
	}
}




